import { request } from "../../utils/http";
let timer= null
let btn = true
Page({
  data: {
    info: {
      bank: "",
      bank_user_name: "",
      card_no: "",
      card_type: "",
      card_type_cn: "",
      is_can_unbind: "",
      life_check: 0,
      mobile: "",
      status: "",
      status_cn: "",
    },
    userInfo: {},
    form: {
      bank_user_name: "",
      id_number: "",
      card_type: "101",
      card_no: "",
      mobile: "",
      is_can_unbind: 0,
      status: 0,
    },
    order_no: "",
    btnText: "",
    currentTime: 60,
    show: false,
    dgtl_envlp: "",
    unique_code: "",
    captcha: "",
    btnCaptchaState: true,
    id: "",
    type:0,
    is_skip_bindcard:0,
    bind:0
  },
  onLoad(query) {
    let order_no = query.order_no
		let id = query.id
    let is_skip_bindcard = my.getStorageSync({key:'is_skip_bindcard'}).data

    if (id) {
      this.setData({
        order_no,
        id,
        type: query.type || 0,
        bind: query.bind || 0,
        is_skip_bindcard
      })
    }
    
    this.getBankCards();
  },
  onShow() {
    my.getStorage({
      key: "userInfo",
      success: (res) => {
        console.log(res);
        if (res.data) {
          this.setData({
            userInfo: res.data,
          });
        }
      },
    });
  },
  getBankCards() {
    request("/member/get_bank_cards", "get").then((res) => {
      if (res.code == 200 && res.data) {
        this.setData({
          form: res.data,
        });
      } else {
        // my.showToast({content: res.message})

      }
    });
  },
  close() {
    this.setData({
      show: false,
    });
  },
  inputCardNo(e){
    let form = this.data.form
    form.card_no = e.detail.value
    this.setData({
      form
    })
  },
  inputMobile(e){
    let form = this.data.form
    form.mobile = e.detail.value
    this.setData({
      form
    })
  },
  inputCaptcha(e){
    this.setData({
      captcha:e.detail.value
    })
  },
  next() {
    let supplier_id = my.getStorageSync({key:'supplier_id'}).data
    let is_skip_bindcard = my.getStorageSync({key:'is_skip_bindcard'}).data

    
    let form = JSON.parse(JSON.stringify(this.data.form));
    if (is_skip_bindcard != 1) {
      if (form.status != 1) {
        if (!this.data.form.card_no) {
          my.showToast({ content: "请输入银行卡号" });
    
          return;
        }
    
        if (!this.data.form.mobile) {
          my.showToast({ content: "请输入银行预留手机号" });
    
          return;
        }
    
        if (!/^1[0-9]\d{9}$/.test(this.data.form.mobile)) {
          my.showToast({ content: "手机号码格式不对！" });
    
          return;
        }
      }
    }
   
    form.bank_user_name = this.data.userInfo.name;
    form.id_number = this.data.userInfo.cert_no;

    form["out_order_no"] = this.data.order_no;
    form["order_id"] = this.data.id;

    if (is_skip_bindcard == 1) {
      request('/member/confirm_bind_card', 'post', {
        out_order_no:this.data.order_no,
        order_id:this.data.id
      }).then(result => {
        if (result.code == 200) {
          my.navigateTo({
            url: `/pages/face-sign-result/face-sign-result?id=${this.data.id}&status=70&type=1` //路径可以使用相对路径或绝对路径的方式进行传递
          })
        }
      })
    } else {
      if(supplier_id == 1) {
        if (form.status == 1) {
          this.bind()
        } else {
          request("/member/bind_bank_card", "post", form).then((res) => {
            if (res.code == 200) {
              this.setData(
                {
                  dgtl_envlp: res.data.dgtl_envlp,
                  unique_code: res.data.unique_code,
                  show: true,
                },
                () => {
                  btn = true
                  this.countTime();
                }
              );
            } else {
              my.showToast({content: res.message})
    
            }
          });
        }
      } else {
        if (form.status == 1) {
          request('/member/confirm_bind_card', 'post', {
            out_order_no:this.data.order_no,
            order_id:this.data.id,
            is_skip_bindcard: this.data.bind == 1 ? 0 : 1
          }).then(result => {
            if (result.code == 200) {
              my.navigateTo({
                url: `/pages/face-sign-result/face-sign-result?id=${this.data.id}&status=70&type=2` //路径可以使用相对路径或绝对路径的方式进行传递
              })
            }
          })
        } else {
          // request("/member/bind_bank_card", "post", form).then((res) => {
          //   if (res.code == 200) {
          //     my.setStorageSync({key:'sign_url',data:res.data.sign_url})
    
          //       my.navigateTo({
          //         url:`/pages/sign-webview/sign-webview?getUrl=1`
          //       })
              
              
          //   } else {
          //     my.showToast({content: res.message})
    
          //   }
          // });
          request("/member/bind_bank_card", "post", form).then((res) => {
            if (res.code == 200) {
              this.setData(
                {
                  dgtl_envlp: res.data.dgtl_envlp,
                  unique_code: res.data.unique_code,
                  show: true,
                },
                () => {
                  btn = true
                  this.countTime();
                }
              );
            } else {
              my.showToast({content: res.message})
    
            }
          });
        }
       
      }
    }
    
    
  },
  countTime() {
    let btnCaptchaState = this.data.btnCaptchaState
    if (!btnCaptchaState) {
      return
    }

    btnCaptchaState = false

    this.setData({
      btnCaptchaState
    })

    timer = setInterval(() => {
      let currentTime = this.data.currentTime
      let btnText = this.data.btnText
      if (currentTime == 0) {
          clearInterval(timer)
          timer = null
          btnCaptchaState = true
          btnText = '重新获取'
          currentTime = 60

          this.setData({
            btnCaptchaState,
            btnText,
            currentTime
          })
    
      }  else {
          this.setData({
            btnText:`${currentTime}s`
          })
      }
      currentTime --
      this.setData({
        currentTime: currentTime
      })
    }, 1000)
  },
  bind() {
    request('/member/confirm_bind_card', 'post', {
      out_order_no:this.data.order_no,
			order_id:this.data.id,
      is_skip_bindcard: this.data.bind == 1 ? 0 : 1
    }).then(res => {
      if (res.code == 200) {
        if (this.data.type == 1) {
          my.navigateTo({
            url:`/pages/face-sign-result/face-sign-result?order_no=${this.data.order_no}&id=${this.data.id}&status=70&type=2`
          })
        } else {
          my.navigateTo({
            url:`/pages/bind-result/bind-result?order_no=${this.data.order_no}&id=${this.data.id}&status=70`
          })
        }
      } else {
        my.navigateTo({
          url:`/pages/bind-result/bind-result?order_no=${this.data.order_no}&id=${this.data.id}&status=60`
        })
      }
    })
  },
  confirm(){
    // if (!btn) {
    //   return
    // }

    if (!this.data.id) {
      my.showToast({ content: "缺少订单号，请重新进入" });
      return
    }

    if (!this.data.captcha) {
      my.showToast({ content: "请输入验证码" });
      return
    }
    btn = false
    request('/member/confirm_bind_card', 'post',{
      dgtl_envlp:this.data.dgtl_envlp,
			unique_code:this.data.unique_code,
			sms_code:this.data.captcha,
			out_order_no:this.data.order_no,
			order_id:this.data.id,
      is_skip_bindcard: this.data.bind == 1 ? 0 : 1
    }).then(res => {
      console.log(res)
      btn = true
      if (res.code == 200) {
        if (this.data.type == 1) {
          my.navigateTo({
            url:`/pages/face-sign-result/face-sign-result?order_no=${this.data.order_no}&id=${this.data.id}&status=70&type=2`
          })
        } else {
          my.navigateTo({
            url:`/pages/bind-result/bind-result?order_no=${this.data.order_no}&id=${this.data.id}&status=70`
          })
        }
        
      } else {
        my.navigateTo({
          url:`/pages/bind-result/bind-result?order_no=${this.data.order_no}&id=${this.data.id}&status=60`
        })
      }
    })
  }
});
